package slidingwindow;

import java.util.HashMap;

public class LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> window = new HashMap<>();
        int left = 0, right = 0;
        int res = 0;
        while (right < s.length()) {
            char c = s.charAt(right);
            right++;
           window.put(c, window.getOrDefault(c, 0) + 1);
           while (window.get(c) > 1) {
               // 窗口缩小
               char d = s.charAt(left);
               left++;
               window.put(c, window.get(c) - 1);
           }

           // 此时窗口已不包含重复元素，故在此更新结果
           res = Integer.max(res, right - left);
        }

        return res;
    }
}
